/*
 * @lc app=leetcode.cn id=128 lang=cpp
 *
 * [128] 最长连续序列
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int longestConsecutive(vector<int> &nums)
    {
        if (nums.size() <= 1)
            return nums.size();
        sort(nums.begin(), nums.end());
        int ans = 1;
        for (int l = 0, r = 1; r <= nums.size(); r++)
        {
            while (r < nums.size() && (nums[r] == nums[r - 1] + 1 || nums[r] == nums[r - 1]))
                r++;

            ans = max(ans, nums[r-1] - nums[l] + 1);
            l = r;
        }
        return ans;
    }
};

// int main()
// {
//     vector<int> nums = {100,4,200,1,3,2};
//     cout << Solution().longestConsecutive(nums) << endl;
//     return 0;
// }

// @lc code=end
